package com.eicas.controller;

import com.eicas.entity.ResultResponse;
import com.eicas.entity.User;
import com.eicas.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author: Administrator
 * @since: 2023/2/9 10:01
 * @description:
 */
@RestController
@RequestMapping("/account")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping
    public String test() {
        return "auth ok";
    }

    @GetMapping("/login")
    public String login(@RequestParam String username, @RequestParam String password) {
        //校验数据库用户名密码
        boolean loginSuccessfully = userService.doLogin(new User(username, password));
        if (loginSuccessfully) {
            //校验成功返回200 返回token
            String token = userService.createToken(username);
            return new ResultResponse(200, token).toString();
        }
        //校验失败返回500
        return new ResultResponse(500, null).toString();
    }
}
